package com.goldgov.kduck.controller;

import com.goldgov.kduck.AuthServerConstants;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

@RestController
public class SimpleController {
//    @GetMapping("/whoami")
//    @ResponseBody
//    public String whoami(@AuthenticationPrincipal Jwt jwt) {
//        return String.format("Hello, %s!", jwt.getSubject());
//    }

    @GetMapping("/")
    public Principal principal(Map map, Principal principal, Authentication authentication){
        return principal;
    }

    @GetMapping("/message")
    public String message() {
        return "secret message";
    }
    @GetMapping(value = "/user")
    @ResponseBody
    public Map<String, Object> user(Principal user, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>();
        if (user instanceof Authentication) {
            Authentication authentication = (Authentication) user;
            request.getSession().setAttribute(AuthServerConstants.SESSION_KEY_LOGINID, "loginid");
            request.getSession().setAttribute(AuthServerConstants.SESSION_KEY_USERID, "USERID");
            request.getSession().setAttribute(AuthServerConstants.SESSION_KEY_USERNAME, "USERNAME");
//            CustomUserDatails principal = (CustomUserDatails) authentication.getPrincipal();
//            IAuthenticatedUser extendAccountInfo = (IAuthenticatedUser) principal.getExtendAccountInfo();
            // 保存用户信息,字符串类型
//            if (StringUtils.isEmpty(extendAccountInfo)) {
//                extendAccountInfo = new AuthenticatedUserImpl("testID", "testLogin", "testName", "testPassword");
//            }
//            request.getSession().setAttribute(AuthServerConstants.SESSION_KEY_LOGINID, extendAccountInfo.getLoginID());
//            request.getSession().setAttribute(AuthServerConstants.SESSION_KEY_USERID, extendAccountInfo.getEntityID());
//            request.getSession().setAttribute(AuthServerConstants.SESSION_KEY_USERNAME, extendAccountInfo.getName());
//            request.getSession().setAttribute(AuthServerConstants.SESSION_KEY_DEPARTID,
//                    extendAccountInfo.getDepartID());
//            request.getSession().setAttribute(AuthServerConstants.SESSION_KEY_SCOPECODE,
//                    extendAccountInfo.getScopeCodes() != null && extendAccountInfo.getScopeCodes().length > 0
//                            ? extendAccountInfo.getScopeCodes()[0]
//                            : "");
//            request.getSession().setAttribute(AuthServerConstants.SESSION_KEY_UNITSCOPECODE,
//                    extendAccountInfo.getUnitScopeCodes() != null && extendAccountInfo.getUnitScopeCodes().length > 0
//                            ? extendAccountInfo.getUnitScopeCodes()[0]
//                            : "");
//            request.getSession().setAttribute(AuthServerConstants.SESSION_KEY_ROLES, principal.getAuthorities().stream()
//                    .map(GrantedAuthority::getAuthority).collect(Collectors.joining(",")));
//            if (request.getAttribute("contentType") == null) {
//                modify by GuoR 2019-07-03 解决在IE下提示JSON下载的问题
            response.setContentType("text/html");
//            }
            result.put("authenticated", authentication.isAuthenticated());
        }
        return result;
    }
}
